package com.cskaoyan.controller.admincontroller;

import com.cskaoyan.bean.po.MarketPermission;
import com.cskaoyan.bean.po.MarketRole;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.chaosbean.role.permissions.vo.PerVO;
import com.cskaoyan.bean.chaosbean.storage.vo.StorageListVO;
import com.cskaoyan.bean.chaosbean.system.vo.SystemOptionsVO;
import com.cskaoyan.service.adminservice.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author jackX
 * @datetime 2022/12/28 18:46
 * @description: TODO:
 */
@RestController
@RequestMapping("admin/role")
public class RoleController {
    @Autowired
    RoleService roleService;

    /**
     * 管理员系统options，get方法
     */
    //http://localhost:8083/admin/role/options
    @RequestMapping("options")
    public BaseRespVo options() {
        SystemOptionsVO optionsVOS = roleService.selectOptions();
        return BaseRespVo.okYa(optionsVOS);
    }

    /**
     * 角色页面显示信息
     *
     * @param info 分页所需的参数信息
     * @param name 根据角色的name来查询对应的数据
     * @return
     */
    //http://localhost:8083/admin/role/list?page=1&limit=20&sort=add_time&order=desc
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo info, String name) {
        //角色管理info页面
        StorageListVO listVO = roleService.selectInfo(info, name);
        return BaseRespVo.okYa(listVO);
    }

    /**
     * 创建角色
     *
     * @param role 创建角色的所需数据
     * @return
     */
    //请求网址: http://182.92.235.201:8083/admin/role/create
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody MarketRole role) {
        BaseRespVo createRole = roleService.create(role);
        return createRole;
    }

    /**
     * 更新角色
     *
     * @param role 更新角色的所需数据
     * @return
     */
    //http://localhost:8083/admin/role/update
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketRole role) {
        BaseRespVo update = roleService.update(role);
        return update;
    }

    /**
     * 删除角色
     *
     * @param role 删除角色的所需数据
     * @return
     */
    //http://182.92.235.201:8083/admin/role/delete
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketRole role) {
        //删除角色
        roleService.delete(role);
        return BaseRespVo.ok();
    }

    /**
     * 查询显示授权管理
     *
     * @param roleId 角色表的ID
     * @return
     */
    //http://182.92.235.201:8083/admin/role/permissions?roleId=72
    @GetMapping("permissions")
    public BaseRespVo permissions(Integer roleId) {
        //参数是角色表的ID
        PerVO vo = roleService.selectPer(roleId);
        return BaseRespVo.okYa(vo);
    }

    //请求网址: http://localhost:8083/admin/role/permissions
    @PostMapping("permissions")
    public BaseRespVo permissions(@RequestBody MarketPermission per) {

        roleService.updateRolePer(per);
        return BaseRespVo.ok();
    }
}

